package com.vjwealth.android.nos.activity.main

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.bumptech.glide.Glide
import com.bumptech.glide.signature.StringSignature
import com.vjwealth.android.nos.Constants
import com.vjwealth.android.nos.R
import com.vjwealth.android.nos.activity.StartActivity
import com.vjwealth.android.nos.adapter.RecyclerViewAdapter
import com.vjwealth.android.nos.base.BaseApplication
import com.vjwealth.android.nos.entity.MapData
import com.vjwealth.android.nos.http.Api
import com.vjwealth.android.nos.http.BaseStringRequest
import com.vjwealth.android.nos.http.VolleyUtils
import com.vjwealth.android.nos.utils.ParamsUtils
import kotlinx.android.synthetic.main.fragment_left_menu.view.*
import kotlinx.android.synthetic.main.layout_left_menu_item.view.*

/**
 * projectName KNOS
 * date 2017/9/29
 * description com.vjwealth.android.nos.activity.main
 */
class MenuLeftFragment : Fragment(){

    val menuName = listOf("消息中心", "客服中心", "邀请好友","绑定车载智能盒", "帮助中心", "关于我们", "退出登录")
    val menuIcon = listOf(R.mipmap.ch_01, R.mipmap.ch_02, R.mipmap.ch_03, R.mipmap.ch_07, R.mipmap.ch_04, R.mipmap.ch_05, R.mipmap.ch_06)

    var menuLeftView: View?= null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        menuLeftView = inflater!!.inflate(R.layout.fragment_left_menu, container, false)
        init()
        return menuLeftView
    }

    private fun init(){
        var initData = BaseApplication.spInfor!!.getInitDataByShared()
        menuLeftView!!.mMenuUserNameView.text = initData!!.nickname
        if("1".equals(initData!!.gender)){
            menuLeftView!!.mMenuUserGenderImage.setImageResource( R.mipmap.nan01)
        }else if("0".equals(initData!!.gender)){
            menuLeftView!!.mMenuUserGenderImage.setImageResource( R.mipmap.nv01)
        }
        var i = System.currentTimeMillis().toString()
        if (TextUtils.isEmpty(initData!!.avatar)) {
            if ("1" == initData!!.gender) {
                Glide.with(this).load(Api.BASE_IMAGE_URL + initData!!.avatar).signature(StringSignature(i))//增加签名
                        .placeholder(ContextCompat.getDrawable(this.activity, R.mipmap.man_avator)).into(menuLeftView!!.mMenuUserIconImage)
            } else {
                Glide.with(this).load(Api.BASE_IMAGE_URL + initData!!.avatar).signature(StringSignature(i))//增加签名
                        .placeholder(ContextCompat.getDrawable(this.activity, R.mipmap.uer_icon)).into(menuLeftView!!.mMenuUserIconImage)
            }
        } else {
            Glide.with(this).load(Api.BASE_IMAGE_URL + initData!!.avatar).signature(StringSignature(i))//增加签名
                    .into(menuLeftView!!.mMenuUserIconImage)
        }
        menuLeftView!!.mMenuVipView.text = initData!!.vipType
        menuLeftView!!.mMenuCarCountView.text = initData!!.carNum + "辆车"

        //初始化菜单
        var menuList: MutableList<MapData> = ArrayList()
        var index = 0
        while (index < 7){
            var mapData = MapData(menuIcon[index], menuName[index], "")
            menuList.add(mapData)
            index++
        }
        var menuAdapter = object : RecyclerViewAdapter<MapData>(this@MenuLeftFragment.context, R.layout.layout_left_menu_item, menuList){

            override fun convert(holder: CommonViewHolder, position: Int, t: MapData) {
                var itemIcon = holder.itemView.mMenuItemIconImage
                var itemName = holder.itemView.mMenuItemNameView
                itemName.text = t.value
                Glide.with(this@MenuLeftFragment.activity).load(t.key).into(itemIcon)
            }
        }
        menuAdapter.onItemClickListener = object : RecyclerViewAdapter.OnRecyclerViewItemClickListener<MapData> {
            override fun onClick(v: View, position: Int, t: MapData) {
                when(position){
                    0 -> toMessageCenterActivity()
                    1 -> toMessageCenterActivity()
                    2 -> toInviteFriendActivity()
                    3 -> toBindBoxActivity()
                    4 -> toHelpActivity()
                    5 -> toAboutUsActivity()
                    6 -> fetchLogout()
                }
            }

        }
        menuLeftView!!.recyclerView.layoutManager = LinearLayoutManager(this@MenuLeftFragment.activity)
        menuLeftView!!.recyclerView.adapter = menuAdapter
    }

    //消息中心
    fun toMessageCenterActivity(){
//        var intent = Intent(this@MenuLeftFragment.activity, NewsActivity::class.java)
//        intent.putExtra("msgType", Constants.NEW_PERSONAL)
//        this@MenuLeftFragment.activity.startActivity(intent)
    }

    //邀请好友
    fun toInviteFriendActivity(){
//        var intent = Intent(this@MenuLeftFragment.activity, InvitationFriendActivity::class.java)
//        this@MenuLeftFragment.activity.startActivity(intent)
    }

    //绑定智能盒子
    fun toBindBoxActivity(){
        val list = BaseApplication.spInfor!!.getHomeCarList()
        if (list != null && list!!.size > 0) {
//            val boxIntent = Intent(this@MenuLeftFragment.activity, BindBoxActivity::class.java)
//            this@MenuLeftFragment.activity.startActivity(boxIntent)
        } else {
            popupCustomerDialog()
        }
    }
     private fun popupCustomerDialog(){}

    //帮助中心
    private fun toHelpActivity(){
//        val helpIntent = Intent(this@MenuLeftFragment.activity, WebViewActivity::class.java)
//        val bundle = Bundle()
//        bundle.putSerializable("url", Api.HELP_CENTER)
//        bundle.putSerializable("title", "帮助中心")
//        bundle.putBoolean("isGoBackFinish", false)
//        helpIntent.putExtras(bundle)
//        this@MenuLeftFragment.activity.startActivity(helpIntent)
    }

    //关于我们
    private fun toAboutUsActivity(){
//        val aboutIntent = Intent(this@MenuLeftFragment.activity, AboutActivity::class.java)
//        this@MenuLeftFragment.activity.startActivity(aboutIntent)
    }

    //退出登录
    private fun fetchLogout(){
        var map = hashMapOf("customerId" to BaseApplication.spInfor!!.getString(Constants.LOGIN_USER_ID))
        var request = BaseStringRequest(Request.Method.POST, Api.logout, ParamsUtils.p_map2string(map),
                Response.Listener { response ->
                    BaseApplication.spInfor!!.clear()
                    val intent03 = Intent(this@MenuLeftFragment.activity, StartActivity::class.java)
                    this@MenuLeftFragment.activity.startActivity(intent03)
                    this@MenuLeftFragment.activity.finish()

                },
                Response.ErrorListener {  })
        VolleyUtils.requestQueue.add(request)
        VolleyUtils.requestQueue.start()
    }
}